package com.example.administrator.myproject.ButtonFragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.administrator.myproject.Adapter.PopwAdapter;
import com.example.administrator.myproject.Adapter.ViPagerAdapter;
import com.example.administrator.myproject.AllFragment;
import com.example.administrator.myproject.Utils.HttpUrl;
import com.example.administrator.myproject.R;
import com.example.administrator.myproject.SearchActivity;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class YanchuFragment extends Fragment {


    private ViewPager mPager;
    private TabLayout mTab;
    private List<Fragment> fragmentList;
    private List<String> nums;
    private ListView lv_pop;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_yanchu, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
//ToolBar
        final Toolbar toolBar = (Toolbar) view.findViewById(R.id.toolBar);
       // toolBar.setNavigationIcon(R.drawable.serach);
        //  toolBar.setTitle("演出");
        // toolBar.inflateMenu(R.menu.main);
        //   toolBar.setOverflowIcon(getResources().getDrawable(R.drawable.menu_show));
        toolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });

        ImageView mIv_bar = (ImageView) view.findViewById(R.id.iv_toobar);
        mIv_bar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupWindow window = new PopupWindow(lv_pop, toolBar.getWidth(), 200);
                window.setBackgroundDrawable(getResources().getDrawable(
                        R.drawable.blue));
                // 设置触摸PopupWindow以外的区域的时候是否消失
                window.setOutsideTouchable(true);
                // 设置PopupWindow获得焦点的能力
                window.setFocusable(true);
                // 作为一个下拉视图来显示.
                window.showAsDropDown(toolBar, 0, 0);
            }
        });
        //六个Fragment放入List中,,再加5个
        fragmentList = new ArrayList<>();
        fragmentList.add(new AllFragment(HttpUrl.AllUrl));
        fragmentList.add(new AllFragment(HttpUrl.SingUrl));
        fragmentList.add(new AllFragment(HttpUrl.MusicUrl));
        fragmentList.add(new AllFragment(HttpUrl.HuajuUrl));
        fragmentList.add(new AllFragment(HttpUrl.WudaoUrl));
        fragmentList.add(new AllFragment(HttpUrl.QuyuanzatanUrl));
        fragmentList.add(new AllFragment(HttpUrl.TiyuUrl));
        fragmentList.add(new AllFragment(HttpUrl.DujiaUrl));
        fragmentList.add(new AllFragment(HttpUrl.ZhoubianUrl));
        fragmentList.add(new AllFragment(HttpUrl.ErtongUrl));
        fragmentList.add(new AllFragment(HttpUrl.DongmanUrl));

        mTab = (TabLayout) view.findViewById(R.id.sliding_tabs);
        mPager = (ViewPager) view.findViewById(R.id.viewpager);
        //设置TabLayout属性
        mTab.setupWithViewPager(mPager);
        mTab.setTabMode(TabLayout.MODE_SCROLLABLE);
        //ViewPager的适配器
        ViPagerAdapter pagerAdapter = new ViPagerAdapter(getFragmentManager(), getContext(),fragmentList);
        //预加载界面的个数
        mPager.setOffscreenPageLimit(fragmentList.size());
        mPager.setAdapter(pagerAdapter);

        //popWidnow
        nums = new ArrayList<String>();
        nums.add("时间范围");
        nums.add("排列方式");
        lv_pop = new ListView(getActivity());
        PopwAdapter popAdapter = new PopwAdapter(getContext(),nums);
        lv_pop.setAdapter(popAdapter);

    }


}
